﻿@model DbTablesModel
@using System.Diagnostics;
@using System.Collections;
@using System.Reflection;
@using System.Runtime.Versioning;
@using NewLife.Common;
@using XCode.DataAccessLayer
@{
    //Layout = NewLife.Cube.Setting.Current.Layout;

    ViewBag.Title = "数据表列表";
}

@foreach (var table in Model.Tables)
{
    <h3>@table.DisplayName（@table.TableName）</h3>
    <table class="table table-bordered table-hover table-striped table-condensed">
        <thead>
            <tr>
                <th class="text-center">名称</th>
                <th class="text-center">显示名</th>
                <th class="text-center">类型</th>
                <th class="text-center">长度</th>
                <th class="text-center">精度</th>
                <th class="text-center">主键</th>
                <th class="text-center">允许空</th>
                <th class="text-center">备注</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var column in table.Columns)
            {
                <tr>
                    <td>@column.ColumnName</td>
                    <td>@column.DisplayName</td>
                    <td class="text-center">@(column.RawType ?? column.DataType?.FullName.TrimStart("System."))</td>

                    @if (column.Length > 0)
                    {
                        <td class="text-center">@column.Length</td>
                    }
                    else
                    {
                        <td></td>
                    }

                    @if (column.Precision > 0 || column.Scale > 0)
                    {
                        <td class="text-center">(@column.Precision, @column.Scale)</td>
                    }
                    else
                    {
                        <td></td>
                    }

                    @if (column.Identity)
                    {
                        <td title="自增" class="text-center">AI</td>
                    }
                    else if (column.PrimaryKey)
                    {
                        <td title="主键" class="text-center">PK</td>
                    }
                    else if (table.Indexes.Any(e => e.Unique && e.Columns.Length == 1 && e.Columns[0].EqualIgnoreCase(column.Name, column.ColumnName)))
                    {
                        <td title="唯一索引" class="text-center">UQ</td>
                    }
                    else
                    {
                        <td></td>
                    }

                    <td class="text-center">@(column.Nullable ? "" : "N")</td>
                    <td>@(column.Description?.TrimStart(column.DisplayName).TrimStart("。", "，"))</td>
                </tr>
            }
        </tbody>
    </table>
    <br />
}
